use "C:\Users\jkbrueck\Documents\STATA\Fuel 4\US data\Calvin3_A_car.dta", clear

drop yy1991-yy2019 airline

gen T1 = 0
gen ct = 0
forvalues i = 1991(1)2018 {
local j = `i' + 1
replace T1 = `i' if y`i' == "A" & y`j' != "A"
replace ct = ct + 1  if y`i' == "A" & (y`j' == "D" | y`j' == "O" | y`j' == "C")
}

tab ct

gen ct2 = 0
forvalues i = 1991(1)2018 {
local j = `i' + 1
replace ct2 = ct2 + 1  if (y`i' == "D" | y`i' == "O" | y`i' == "C") & y`j' == "A"
}

tab ct2

drop if ct2 == 1

gen ct3 = 0
forvalues i = 1991(1)2018 {
local j = `i' + 1
replace ct3 = ct3 + 1  if y`i' == "A" & y`j' == "S"
}

tab ct3

gen ct4 = 0
forvalues i = 1991(1)2018 {
local j = `i' + 1
replace ct4 = ct4 + 1  if (y`i' == "A" | y`i' == "D" | y`i' == "O" | y`i' == "C") & y`j' == "S"
}

tab ct4

gen ct4a = 0
forvalues i = 1991(1)2018 {
local j = `i' + 1
replace ct4a = ct4a + 1  if (y`i' == "D" | y`i' == "O" | y`i' == "C") & y`j' == "S"
}

tab ct4a

gen scrap_yr = .
forvalues i = 1991(1)2018 {
local j = `i' + 1
replace scrap_yr = `i' if (y`i' == "A" | y`i' == "D" | y`i' == "O" | y`i' == "C") & y`j' == "S"
}

tab scrap_yr

gen scrapped = 0
forvalues i = 1991(1)2018 {
replace scrapped = 1 if y`i' == "S" 
}

tab scrapped


gen end_A = 0
replace end_A = 1 if y2019 == "A"
gen end_other = 0
replace end_other = 1 if y2019 == "D" | y2019 == "O" | y2019 == "C"

tab end_A
tab end_other

gen lifespan = scrap_yr - ff2
tab lifespan

collapse (mean) lifespan, by(type)
save "C:\Users\jkbrueck\Documents\STATA\Fuel 4\US data\mean lifespans.dta", replace











